// pages/collect/collect.js
import IndexModel from '../../models/IndexModel';
const indexModel = new IndexModel();
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type: '101',
    collectList: [],
    page: 1,
    page_total: 0,
    showPackage: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log('collect->', options);
    this.setData({
      type: options.type,
      showPackage: options.type === '102'
    });
    this.btnHandle(1, true);
  },

  btnHandle(page, override) {
    switch (this.data.type) {
      case '101':
        this.fetchCollectWebsite(page, override)
        break;
      case '102':
        this.fetchCollectPackage(page, override)
        break;
      case '103':
        this.fetchCollectInfo(page, override)
        break;

      default:
        break;
    }
  },

  selectBtn(e) {
    const {
      currentTarget: {
        dataset: {
          param,
        },
      },
    } = e;
    this.setData({
      type: param,
    })
    this.btnHandle(1, true);
  },

  fetchCollectWebsite(page, override) {
    wx.showLoading({
      title: '加载中',
    })
    this.loading = true;
    console.log('collect-id-66-->', app.globalData.userId);
    indexModel.GetCollectWebsite(page, app.globalData.userId).then(res => {
      
      const list = res.ListJson.List;
      const page_total = res.ListJson.Result.TotalPageCount;
      this.setData({
        page,
        page_total: Number(page_total),
        collectList: override ? list : this.data.collectList.concat(list),
      })
      wx.hideLoading();
      this.loading = false;
    }).catch(e => {
      console.error(e)
      this.loading = false;
    })
  },
  fetchCollectInfo(page, override) {
    wx.showLoading({
      title: '加载中',
    })
    this.loading = true;
    indexModel.GetCollectInfo(page, app.globalData.userId).then(res => {
      const list = res.ListJson.List;
      const page_total = res.ListJson.Result.TotalPageCount;
      this.setData({
        page,
        page_total: Number(page_total),
        collectList: override ? list : this.data.collectList.concat(list),
      })
      // console.log('collectList->', this.data.collectList)
      wx.hideLoading();
      this.loading = false;
    }).catch(e => {
      console.error(e)
      this.loading = false;
    })
  },
  fetchCollectPackage(page, override) {
    wx.showLoading({
      title: '加载中',
    })
    this.loading = true;
    indexModel.GetCollectPackage(page, app.globalData.userId).then(res => {
      const list = res.ListJson.List;
      const page_total = res.ListJson.Result.TotalPageCount;
      this.setData({
        page,
        page_total: Number(page_total),
        collectList: override ? list : this.data.collectList.concat(list),
      })
      // console.log('collectList->', this.data.collectList)
      wx.hideLoading();
      this.loading = false;
    }).catch(e => {
      console.error(e)
      this.loading = false;
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (!this.loading && this.data.page < this.data.page_total) {
      this.btnHandle(this.data.page + 1, false)
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})